import 'package:flutter/material.dart';
import 'package:luckiness/utils/model/order_info.dart';
import 'package:provider/provider.dart';
import '../utils/general.dart';
import '../components/ffbtn.dart';
import './ffcartnumber.dart';
class FFGoodStyle {

  static Widget topImaggView(BuildContext context) {
    double scale = widthScale(context);
    Container con = Container(
      height: 150 * scale,
      child: Stack(
        fit:StackFit.expand,
        children:[
          Image.asset('assets/images/menu/dialog1.jpg', fit:BoxFit.fitWidth),          
          Positioned(
            top: 0,
            right: 0,
            child: GestureDetector(
              onTap:(){
                FFGoodStyle.removePopView();
              },
              child:SizedBox(
                width: 40 * scale,
                height: 40 * scale,
                child: Center(
                  child: Icon(Icons.cancel, size:20 * scale, color:Colors.white),
                ),
              ),
            )
          ),
          Positioned(
            bottom: 0,
            left: 0,            
            child: Container(
              padding:EdgeInsets.fromLTRB(15 * scale, 0, 0, 15 * scale),
              child:Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('标准美式', style:TextStyle(fontSize: 22 * scale, color:Colors.white, fontWeight:FontWeight.bold, decoration: TextDecoration.none)),
                  Text('Americano', style:TextStyle(fontSize: 14 * scale, color:Colors.white, decoration: TextDecoration.none, fontWeight:FontWeight.normal)),
                ],
              )
            ))
        ]
      ),
    );
    return con;
  }

  static Widget coffeeSpecialView(BuildContext context, String title, List list) {
    int count = list.length;
    double scale = widthScale(context);
    int rowNum = count % 3 == 0 ? (count ~/ 3) : (count ~/ 3 + 1);
    Container con = Container(
      margin: EdgeInsets.symmetric(vertical:15 * scale),
      height: (30 * rowNum + 15 * (rowNum - 1)) * scale,
      child: Row(
        crossAxisAlignment:CrossAxisAlignment.start,
        children:[
          SizedBox(
            width: 65 * scale,
            child: Padding(
              padding: EdgeInsets.only(left:15 * scale),
              child:Text(title, style:TextStyle(fontSize:14 * scale, color:Color.fromRGBO(56, 56, 56, 1.0), decoration:TextDecoration.none)),                      
            ),
          ),
          Expanded(                    
            child:GridView.builder(
              padding: EdgeInsets.all(0),
              physics: NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 80 * scale, 
                mainAxisSpacing:15 * scale, 
                crossAxisSpacing:10 * scale,                
                childAspectRatio: ffSavePointNumber(8 / 3, 2),
              ), 
              itemBuilder: (BuildContext context, int inex){
                return GestureDetector(
                  onTap:(){},
                  child:Container(
                    decoration: BoxDecoration(
                      color:Color.fromRGBO(204, 192, 180, 1.0),
                      borderRadius:BorderRadius.circular(15 * scale),                              
                    ),
                    child: Center(
                      child:Text(list[inex], style:TextStyle(fontSize:14 * scale, color:Colors.white, decoration: TextDecoration.none))
                    ),)
                );
              }, 
              itemCount: count,)
          ),
        ]
      ),
    );
    return con;
  }

  static Widget specialView(BuildContext context) {

    double scale = widthScale(context);
    List titleList = productStyle().keys.map((e)=>e).toList();
    List styleList = productStyle().values.map((e)=>e).toList();
    SizedBox box = SizedBox(
      height:screenHeight(context) - (88 + 270) * scale,
      child: SingleChildScrollView(
        child:Column(
          children: <Widget>[
            FFGoodStyle.coffeeSpecialView(context, titleList[0], styleList[0]),
            FFGoodStyle.coffeeSpecialView(context, titleList[1], styleList[1]),
            FFGoodStyle.coffeeSpecialView(context, titleList[2], styleList[2]),
            FFGoodStyle.coffeeSpecialView(context, titleList[3], styleList[3]),
            Divider(color:separatorLineColor, height: 0,),
            Container(
              padding: EdgeInsets.all(15 * scale),
              child:Column(
                crossAxisAlignment:CrossAxisAlignment.start,
                mainAxisSize:MainAxisSize.min,
                children: <Widget>[
                  Text('产品介绍', style:TextStyle(fontSize:13 * scale, color:Color.fromRGBO(56, 56, 56, 1.0), fontWeight:FontWeight.normal, decoration: TextDecoration.none)),
                  Row(
                    mainAxisSize:MainAxisSize.min,
                    children:[
                      Expanded(
                        child: Text('我会一直传承意大利人数百年的做法，用咖啡发我会一直传承意大利人数百年的做法，用咖啡发', softWrap: true, maxLines:3, style:TextStyle(fontSize:12 * scale, decoration: TextDecoration.none, color:Colors.black26, fontWeight: FontWeight.normal)),
                      )
                    ]
                  )
                ],
              )
            )
          ],
        ),
      ),
    );
    return box;
  }

  static Widget bottomView(BuildContext context) {
    GlobalKey<FFCartNumberState> tempState = GlobalKey<FFCartNumberState>();
    double scale = widthScale(context);
    SizedBox box = SizedBox(
      height: 120 * scale,
      child:Column(
        children:[
          Divider(height:1 * scale),
          Container(
            padding: EdgeInsets.symmetric(horizontal:15 * scale),
            height: 59 * scale,
            child: Row(
              mainAxisAlignment:MainAxisAlignment.spaceBetween,
              children:[
                Column(
                  mainAxisSize:MainAxisSize.min,
                  crossAxisAlignment:CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('Y21', style:TextStyle(fontSize: 18 * scale, fontWeight: FontWeight.bold, color:Colors.black87, decoration: TextDecoration.none)),
                    Text('标准美式', style:TextStyle(fontSize: 12 * scale, fontWeight: FontWeight.normal, color:Colors.black38, decoration: TextDecoration.none)),
                  ],
                ),
                FFCartNumber(key: tempState,),
              ]
            ),          
          ),
          Divider(height:1 * scale),
          Container(
            height: 59 * scale,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children:[
                FFRowBtn(bgColor:Colors.blue[200], borderColor:Colors.white, title:'充二赠一', width:85 * scale, onPress: (){                 
                  print(tempState.currentState.number);
                  OrderInfo info = OrderInfo(name:'香草拿铁', appearance: '大/常温/双份奶', prompt: '仅限打包', price:
                  27, number: 1);
                  Provider.of<FFChangeNotifier>(context, listen: false).updateOrderInfo = info;
                },),
                FFRowBtn(bgColor:Colors.blue[200], borderColor:Colors.white, title:'收藏口味', width:85 * scale,),
                FFRowBtn(bgColor:Colors.blue[200], borderColor:Colors.white, title:'加入购物车', width:85 * scale),
              ]
            ),
          )
        ],
      ),
    );
    return box;
  }

  static OverlayState overlayState;
  static OverlayEntry entry;

  static void removePopView() {
    entry?.remove();
  }

  static showPopView(BuildContext context) {
      double scale = widthScale(context);

      overlayState = Overlay.of(context);
      entry = OverlayEntry(builder: (BuildContext context){
        Center cen = Center(
          child:Container(
            // height: screenHeight(context),
            decoration: BoxDecoration(
              color: Colors.white,              
            ),
            child: Column(
              children:[
                FFGoodStyle.topImaggView(context),
                FFGoodStyle.specialView(context),
                FFGoodStyle.bottomView(context),
              ]
            ),
          )
        );
        return Container(
          color: Color.fromRGBO(0, 0, 0, 0.5),
          padding:EdgeInsets.fromLTRB(20 * scale, 44 * scale, 20 * scale, 44 * scale),
          child:PhysicalModel(
            color: Colors.transparent,
            clipBehavior: Clip.antiAlias,
            borderRadius: BorderRadius.circular(20.0),
            child: cen,
          ),
        );
      });

      overlayState?.insert(entry);
  }
  
  static ffShow(BuildContext context) {
    FFGoodStyle.showPopView(context);
  }
}